package com.i8office.android.zhihuixinwu.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cyan.tools.annotation.ActivityFragmentInject;
import com.cyan.tools.base.BaseActivity;
import com.gyf.barlibrary.ImmersionBar;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.adapter.NewsAttachmentAdapter;
import com.i8office.android.zhihuixinwu.bean.NewsAttachment;
import com.i8office.android.zhihuixinwu.module.contract.AttachmentContract;
import com.i8office.android.zhihuixinwu.module.presenter.AttachmentPresenter;
import com.i8office.android.zhihuixinwu.widget.ItemDecoration;

import java.util.ArrayList;
import java.util.List;

import bean.NewsItem;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import view.ThreePointLoadingView;

@ActivityFragmentInject(
        contentViewId = R.layout.activity_attachment
)
public class NewsDetailActivity extends BaseActivity<AttachmentContract.Presenter> implements AttachmentContract.View, WebViewFragment.OnGetPageTitle {
    public static String from = "office";
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.bottom_layout)
    View bottomLayout;
    @BindView(R.id.loading)
    ThreePointLoadingView loading;
    @BindView(R.id.title)
    TextView tvTitle;
    private ActionBarDrawerToggle mDrawerToggle;
    NewsAttachmentAdapter adapter;
    NewsItem item = null;
    List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        from = getIntent().getStringExtra("from");
        /*mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        drawerLayout.addDrawerListener(mDrawerToggle);*/
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f) //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                .init();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        adapter = new NewsAttachmentAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new ItemDecoration(this, 1));
        recyclerView.setAdapter(adapter);
        mPresenter = new AttachmentPresenter(this);
        item = (NewsItem) getIntent().getSerializableExtra("item");
        mPresenter.getAttachments(item.InfoID);
    }

    @OnClick({R.id.index})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.index:
                drawerLayout.openDrawer(Gravity.LEFT);
                break;
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                if (this.getClass() != MainActivity.class)
                if (drawerLayout.isDrawerOpen(Gravity.START)) {
                    drawerLayout.closeDrawers();
                } else {
                    finish();
                }
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showAttachments(List<NewsAttachment> attachments) {
        if (attachments != null && attachments.size() > 0) {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            bottomLayout.setVisibility(View.VISIBLE);
        }else{
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            bottomLayout.setVisibility(View.GONE);
        }
        List<NewsAttachment> attachmentsBeans = new ArrayList<>();
        NewsAttachment first = new NewsAttachment();
        first.FileName = item.Title;
        first.type = 1;
        attachmentsBeans.add(first);
        if (attachments != null)
            attachmentsBeans.addAll(attachments);

        adapter.addAll(attachmentsBeans);
        adapter.setOnItemClick(new NewsAttachmentAdapter.OnItemClick() {
            @Override
            public void onItemClick(int position) {
                viewpager.setCurrentItem(position);
                drawerLayout.closeDrawers();
            }
        });
        viewpager.setOffscreenPageLimit(0);
        for (int i = 0; i < attachmentsBeans.size(); i++) {
            Fragment fragment = null;
            Bundle bundle = new Bundle();
            switch (i) {
                case 0:
                    fragment = new WebViewFragment();
                    bundle.putString("url", item.Url);
                    bundle.putString("title", item.Title);
                    fragment.setArguments(bundle);
                    break;
                default:
                    fragment = new NewsAttachmentFragment();
                    bundle.putString("id", attachmentsBeans.get(i).AttachId);
                    bundle.putString("fileName", attachmentsBeans.get(i).FileName);
                    fragment.setArguments(bundle);
                    break;
            }
            fragments.add(fragment);
        }

        viewpager.setAdapter(new MyPageAdapter(getSupportFragmentManager()));
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvTitle.setText(adapter.getData().get(position).FileName);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    /**
     * Created by jiajia on 2018/3/15.
     */
    public class MyPageAdapter extends FragmentStatePagerAdapter {
        private FragmentManager mFManager;

        public MyPageAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
            mFManager = fragmentManager;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            this.mFManager.beginTransaction().show(fragment).commit();
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            Fragment fragment = fragments.get(position);
            mFManager.beginTransaction().hide(fragment).commit();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }


        @Override
        public int getCount() {
            return fragments == null ? 0 : fragments.size();
        }
    }

    @Override
    public void showProgress() {
        loading.play();
    }

    @Override
    public void hideProgress() {
        loading.stop();
    }

    @Override
    public void onGetPageTitle(String title) {
        tvTitle.setText(item.Title);
    }
}
